﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Lottery.Herper;
using Lottery.MediaData.Core;

namespace Lottery.MediaData.UI {
    /// <summary>
    /// 选择足彩期数
    /// </summary>
    public partial class SelectIndexForm : Form {
        public SelectIndexForm() {
            InitializeComponent();

            this.SetDialogStyle();

            DataHelper.CheckDirectory();

            GetIndexList(string.Empty);
        }

        private void GetIndexList(string qiShu) {
            var data = DataHelper.GetAllIndexes();
            listBoxIndex.Items.Clear();
            listBoxIndex.Items.AddRange(data);
            if (listBoxIndex.Items.Count > 0) {
                if (string.IsNullOrEmpty(qiShu))
                    listBoxIndex.SelectedIndex = 0;
                else
                    listBoxIndex.SelectedIndex = listBoxIndex.Items.IndexOf(qiShu);
            }
        }

        /// <summary>
        /// 期数
        /// </summary>
        public string QiShu { get; set; }

        private void button2_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
        }

        private void buttonAdd_Click(object sender, EventArgs e) {
            NewIndexForm ni = new NewIndexForm();
            if (ni.ShowDialog() == DialogResult.OK) {
                GetIndexList(ni.QiShu);
            }
        }

        protected virtual bool DoOK() {
            QiShu = listBoxIndex.Items[listBoxIndex.SelectedIndex].ToString();
            return true;
        }
        private void buttonOK_Click(object sender, EventArgs e) {
            if (listBoxIndex.SelectedIndex == -1) {
                this.ShowInfo("没有选择期数");
                return;
            }

            if (editMaxCount.Visible && MaxCount == -1) {
                this.ShowInfo("没有选择场次总数");
                editMaxCount.Focus();
                return;
            }

            if (DoOK())
                DialogResult = DialogResult.OK;
        }

        public void ShowMaxCount() {
            labelMaxCount.Visible = true;
            editMaxCount.Visible = true;

            editMaxCount.Items.Clear();
            for (var i = 3; i <= 14; i++) {
                editMaxCount.Items.Add(i.ToString());
            }
        }

        public int MaxCount {
            get {
                if (editMaxCount.SelectedIndex == -1) 
                    return -1;
                else
                    return editMaxCount.SelectedIndex + 3;
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e) {
            // 删除期数
            if (listBoxIndex.Items.Count == 0) {
                this.ShowInfo("没有期数可以删除");
                
                return;
            }
            if (listBoxIndex.SelectedIndex == -1) {
                this.ShowInfo("请选择要删除的期数");
                return;
            }
            var qiShu = listBoxIndex.Items[listBoxIndex.SelectedIndex].ToString();
            var info = string.Format("请确认是否删除期数：{0} ?", qiShu);
            if (this.ShowYesNoInfo(info) != DialogResult.Yes) {
                return;
            }
            info = string.Format("因为删除后数据不能恢复，请再次确认是否删除期数：{0} ?", qiShu);
            if (this.ShowYesNoInfo(info) != DialogResult.Yes) {
                return;
            }

            //删除操作
            DataHelper.DeleteIndex(qiShu);
            listBoxIndex.Items.RemoveAt(listBoxIndex.SelectedIndex);
        }
    }
}
